import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int x: list) {

        }

    }
}
class Solution3 {
    public long minimumPossibleSum(int n, int target) {
        long sum = 0;
        int i = 1;
        int k = 0;
        while(k < n){
            if(i <= target-i && k < n){
                sum+=i;
                k++;
            }
            if(i >= target && k < n){
                sum += i;
                k++;
            }
            i++;
        }
        return sum;
    }
}
class Solution1 {
    public int furthestDistanceFromOrigin(String moves) {
        int[] n = new int[3];
        for(int i=0; i<moves.length(); i++){
            if(moves.charAt(i) == 'L'){
                n[0]++;
            }else if(moves.charAt(i) == 'R'){
                n[1]++;
            }else{
                n[2]++;
            }
        }
        return Math.max(n[0],n[1])+n[2]-Math.min(n[0],n[1]);
    }
}
class Solution {
    /*
    1.当sum < target时,肯定不满足条件，返回-1
    2.当sum == target时，直接返回0
    3.当sum > target时，

    */
    public int minOperations(List<Integer> nums, int target) {
        long sum = 0;
        //31是根据数据范围确定，从前往后依次是2^0 2^1....
        int[] cnt = new int[31];
        for(int x : nums){
            sum += x;
            for(int i=0; i<31; i++){
                //类似于哈希，记录哪个数字有几个
                cnt[i] += x >> i & 1;
            }
        }
        if(sum < target) return -1;
        int i = 0, ans = 0, s = 0;
        while(1L<<i <= target){
            s += cnt[i]*(1<<i);
            int mask = (1<<(i+1))-1;
            //j=i
            i += 1;
            if(s >= (target&mask)){
                continue;
            }
            ans += 1;//当前2^j在nums中不能通过累加或直接得到
            while(cnt[i] == 0){//在nums中找到大于2^j的数，然后一路分割
                ans += 1;
                i += 1;
            }
        }
        return ans;
    }
}